﻿using ArticleandSiteData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SplitViewOptions.BusinessControl
{
    public sealed partial class ShareUserControl : UserControl
    {
        public ShareUserControl()
        {
            this.InitializeComponent();
            this.Loaded += ShareUserControl_Loaded;
           
        }

        private string _mode = "site";
        private List<WebShareinfo> _resultlist = null;
        private async void ShareUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Shareinfo sharelist = await BmobWebsiteData.GetLists<Shareinfo>(App.ShareSiteorArticle);
                if (sharelist == null) return;
                _resultlist = sharelist.results.ToList<WebShareinfo>();
                shareviewer.ItemsSource = _resultlist.Where(m => m.type == "site").Reverse();
            }
           catch(Exception ex)
            {

            }
        }
     
        private async void addbtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string userid = string.Empty;
                string username = string.Empty;
                if (App.Loginuser == null)
                {

                    userid = "unknowuserid" + Guid.NewGuid();
                    username = "unkownusename" + Guid.NewGuid();
                }


                if (App.Loginuser != null)
                {
                    userid = App.Loginuser.objectId;
                    username = App.Loginuser.username;
                }
                if (string.IsNullOrEmpty(urlbox.Text))
                {
                    _mode = "text";
                }
                if (!urlbox.Text.StartsWith("http") || !urlbox.Text.StartsWith("www"))
                {
                    _mode = "text";
                }
                //TODO：不要自己写  ——林德熙
                var jsonstr = "{" + "\"" + "url" + "\"" + ":" + "\"" + urlbox.Text + "\"" + "," + "\"" + "name" + "\"" + ":" + "\"" + namebox.Text + "\"" + "," + "\"" + "userid" + "\"" + ":" + "\"" + userid + "\"" + "," + "\"" + "username" + "\"" + ":" + "\"" + username + "\"" + "," + "\"" + "type" + "\"" + ":" + "\"" + _mode + "\"" + "," + "\"" + "keyword" + "\"" + ":" + "\"" + string.Empty + "\"" + "}";
                StringContent content = new StringContent(jsonstr);
                HttpResponseMessage oneHttpResponseMessage = await BmobWebsiteData.AddOne<HttpResponseMessage>(App.ShareSiteorArticle, jsonstr);
                if (oneHttpResponseMessage.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    await App.showmessage("分享成功");
                    //MessageDialog one = new MessageDialog("分享成功");
                    //await one.ShowAsync();
                }
                else
                {
                    await App.showmessage("分享失败");
                    //MessageDialog one = new MessageDialog("分享失败");
                    //await one.ShowAsync();
                }
            }
            catch(Exception ex)
            {

            }
        }

        private void sites_Click(object sender, RoutedEventArgs e)
        {
            _mode = "site";
            if (_resultlist == null) return;
            shareviewer.ItemsSource = _resultlist.Where(m => m.type == "site").Reverse();
        }

        private void article_Click(object sender, RoutedEventArgs e)
        {
            if (_resultlist == null) return;
            _mode = "article";
            shareviewer.ItemsSource = _resultlist.Where(m => m.type == "article").Reverse();
        }

        private void text_Click(object sender, RoutedEventArgs e)
        {
            _mode = "text";
            if (_resultlist == null) return;
            shareviewer.ItemsSource = _resultlist.Where(m => m.type == "text").Reverse();
        }
        private void Border_Tapped(object sender, TappedRoutedEventArgs e)
        {
            App.Readmode = false;
            App.Isdownloadstirngmode = false;
            Border obj = sender as Border;
            WebShareinfo oneinfo = obj?.DataContext as WebShareinfo;
            if (oneinfo != null && oneinfo.type != "text")
            {
                if (!string.IsNullOrEmpty(oneinfo.url))
                {
                    App.Currenturl = new Uri(oneinfo.url, UriKind.RelativeOrAbsolute);
                    App.Mainframe.Navigate(typeof(HomePage));
                }
                //MainPage.
                //Frame rootFrame = Window.Current.Content as Frame;
                //if (rootFrame != null)
                //{
                //    App.currenturl = new Uri(oneinfo.url, UriKind.RelativeOrAbsolute);
                //    rootFrame.Navigate(typeof(HomePage));
                //    Window.Current.Content = rootFrame;
                //    Window.Current.Activate();
                //}            


            }
        }

        private void SymbolIcon_Tapped(object sender, TappedRoutedEventArgs e)
        {
            App.Mainframe.Navigate(typeof(ShareTextPage));
        }
    }
}
